package month4.day27;

import java.io.*;

public class S4 {
    public static void main(String[] args) {
        //实现文件加密解密：加密规则是读出每一个字节，取反(~)后，写入
        //新文件，然后删除原文件，把新文件改成原文件同名的文件，
        //解密规则类似。输入一个文件名，实现加密和解密功能
        File file = new File("E:\\Java8\\新建文件夹\\java78.txt");
        File file2 = new File("E:\\Java8\\新建文件夹\\java78(1).txt");
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(file);
            os = new FileOutputStream(file2);
            int l;
            while ((l = is.read()) != -1) {
                int temp = l;
                l = (byte) (~temp);
                os.write(l);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
                os.close();
                System.out.println(file.delete());
                System.out.println(file2.renameTo(file));
                System.out.println("加密成功");
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}
